#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# volume_control.py
#
#  Copyright (c) 2007 INdT (Instituto Nokia de Technologia)
#                2007 Kenneth Rohde Christiansen
#
#  Portions of the code, copyright (c) the Gajim Project (GPL)
#
#  Author: Kenneth Rohde Christiansen <kenneth.christiansen@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import icon_theme

from gettext import gettext as _

class VolumeControl(gtk.ToggleButton):

    CLICK_TIMEOUT = 2000

    def __init__(self, volume=0.0):
        gtk.ToggleButton.__init__(self)

        self.volume = volume

        self.set_image(self._get_volume_image())
        self.connect('clicked', self.on_volume_button_clicked)

        # Volume Bar
        self.volume_bar = gtk.Window(gtk.WINDOW_POPUP)
        self.volume_bar.set_default_size(-1, 200)
        self.volume_bar.connect('focus-out-event', self.on_focus_out_event)
        self.volume_bar.connect('event', self.on_event)

        self.vscale = gtk.VScale(gtk.Adjustment(self.volume, 0, 1, 0.05, 0.1, 0))
        self.vscale.set_inverted(True)
        self.vscale.set_draw_value(False)
        self.vscale.connect('change-value', self.on_volume_scale_change_value)
        self.vscale.connect('button-release-event', self.on_volume_scale_button_release_event)
        self.vscale.show()

        frame = gtk.Frame()
        frame.set_border_width(0)
        frame.set_shadow_type(gtk.SHADOW_IN)
        frame.add(self.vscale)
        frame.show()

        self.volume_bar.add(frame)


    def on_volume_scale_change_value(self, widget, scroll, value):
        self.set_volume(value)

        return False


    def on_volume_scale_button_release_event(self, widget, event, data=None):
        def do_hide_vscale():
            print 'fuks: ',
            print self.vscale.is_focus()
            self.set_active(False)
            self.on_volume_button_clicked(self)

        gobject.timeout_add(VolumeControl.CLICK_TIMEOUT, do_hide_vscale)


    def on_volume_button_clicked(self, button):
        if self.get_active():
            self.show_volume_scale()
        else:
            self.volume_bar.hide()


    def set_volume(self, volume=0.5):
        self.volume = volume
        self.vscale.set_value(volume)
        volume_image = self._get_volume_image()
        self.set_image(volume_image)

        print '[volume_control: set_volume()]: ',
        print volume


    def show_volume_scale(self):
        window = self.window
        win_x, win_y = window.get_origin()
        win_x += self.allocation.x
        win_y += self.allocation.y

        width = self.allocation.width
        height = self.allocation.height

        bar_width, bar_height = self.volume_bar.size_request()
        dx = (width - bar_width) / 2

        self.volume_bar.move(win_x + dx, win_y + height)
        self.volume_bar.show()


    def hide_volume_scale(self):
        self.set_active(False)
        self.on_volume_button_clicked(self)


    def _get_volume_icon(self):
        if self.volume <= 0.0:
            icon_name = 'audio-volume-muted'
        elif self.volume > 0.0 and self.volume < 0.3:
            icon_name = 'audio-volume-low'
        elif self.volume >= 0.3 and self.volume <= 0.7:
            icon_name = 'audio-volume-medium'
        elif self.volume > 0.7:
            icon_name = 'audio-volume-high'

        return icon_name


    def _get_volume_image(self):
        itheme = icon_theme.icon_theme_get_default()
        image = gtk.Image()
        image.set_from_pixbuf(itheme.load_icon(self._get_volume_icon(), 16, 0),)

        return image


    def on_focus_out_event (self, widget, event, data=None):
        print 'hide me'

        return False


    def on_event (self, widget, event, data=None):
        print 'on_event'
        print event
        print event.type


def test():
    win = gtk.Window()
    win.connect('destroy', gtk.main_quit)

    volume_control = VolumeControl(50)

    hbox = gtk.HBox(False, 0)
    hbox.pack_end(volume_control, False)
    hbox.pack_end(gtk.Label('Testing...'))

    win.add(hbox)
    win.show_all()

    gtk.main()

if __name__ == '__main__':
    test()

